<?php

namespace LowCode\UI;

use Illuminate\Support\Collection;
use LowCode\Support\JsExp;

/**
 * @method static static make() AnalysisChartCard组件
 */
class AnalysisChartsTabs extends Node
{

    /**
     * @param string $label
     * @param Node $slotTab
     * @param string|null $value
     * @return $this
     */
    public function tab(string $label, Node $slotTab, string $value = null): static
    {
        $props = $this->props??[];
        $tabs = $props['tabs']??[];
        if (!$value){
            $value = "tab_".count($tabs);
        }
        $tabs[] = compact('label','value');
        return $this->tabs($tabs)->addSlot($slotTab, $value);
    }

    /**
     * @param string $tabValue
     * @return $this
     */
    public function removeTab(string $tabValue): static
    {
        $tabs = $this->get("tabs",[]);
        $key = Collection::make($tabs)->search(function ($item) use ($tabValue){
            return $tabValue == $item['value'];
        });
        if ($key!==false){
            unset($tabs[$key]);
        }
        return $this;
    }

    /**
     * @param array|JsExp $tabs
     * @return $this
     */
    public function tabs(array|JsExp $tabs): static
    {
        return $this->prop("tabs",$tabs);
    }
}
